package com.example.kermit.zhpaper.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.kermit.zhpaper.R;
import com.example.kermit.zhpaper.entity.ItemInfo;
import com.example.kermit.zhpaper.http.MySingleRequestQueue;

import java.util.IllegalFormatCodePointException;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Kermit on 15-7-25.
 * e-mail : wk19951231@163.com
 */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {

    private final LayoutInflater inflater;
    private Context context;
    private List<ItemInfo.Story> stories;
    private ImageLoader imageLoader;
    private MyItemClickListener clickListener;
    private MyItemLongClickListener longClickListener;

    public List<ItemInfo.Story> getData(){
        return stories;
    }

    public void setData(List<ItemInfo.Story> stories){
        this.stories = stories;
    }

    public interface MyItemClickListener{
        void onItemClick(View view, @Nullable int position, int newsId);
    }

    public interface MyItemLongClickListener{
        void onItemLongClick(View view, @Nullable int position);
    }

    public MyRecyclerViewAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
        imageLoader = new MySingleRequestQueue(context).getImageLoader();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.paper_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        view.setOnClickListener(myViewHolder);
        view.setOnClickListener(myViewHolder);
        return myViewHolder;
    }


    public void setClickListener(MyItemClickListener listener){
        clickListener = listener;
    }

    public void setLongClickListener(MyItemLongClickListener listener){
        longClickListener = listener;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(stories.get(position).getTitle());
        Log.w("kermit", stories.get(position).getTitle());
        holder.networkImageView.setImageUrl(stories.get(position).getImages().get(0), imageLoader);
        Log.w("kermit", stories.get(position).getImages().get(0));
        holder.newsId = stories.get(position).getId();
    }

    @Override
    public int getItemCount() {
        return stories.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        @Bind(R.id.id_item_img)
        NetworkImageView networkImageView;
        @Bind(R.id.id_item_title)
        TextView textView;
        int newsId;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        @Override
        public void onClick(View v) {
            if (clickListener != null){
                clickListener.onItemClick(v, getAdapterPosition(), newsId);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (longClickListener != null) {
                longClickListener.onItemLongClick(v, getAdapterPosition());
            }
            return true;
        }

    }
}
